require 'math'

module OF7
	module Map
		class Vector
		
			attr_accessor :x, :y, :z
			
			def initialize(x, y, z)
				@x, @y, @z = x, y, z
			end
			
			def +(other)
				return self.new(@x + other.x, @y + other.y, @z + other.z)
			end
			
			def -(other)
				return self.new(@x - other.x, @y - other.y, @z - other.z)
			end
			
			def *(other)
				if other.kind_of? Numeric then
					return self.new(@x * other, @y * other, @z * other)
				else
					return (@x * other.x) + (@y * other.y) + (@z * other.z)
				end
			end
			
			def /(other)
				if other.kind_of? Numeric then
					return self.new(@x / other, @y / other, @z / other)
				else
					nx = @y * other.z - @z * other.y
					ny = @z * other.x - @x * other.z
					nz = @x * other.y - @y * other.x
					return self.new(nx, ny, nz)
				end
			end
			
			def length
				return sqrt(@x ** 2 + @y ** 2 + @z ** 2)
			end
			
			def get_theta
				return atan2(@x, @y)
			end
			
			def get_phi
				return acos(@z / length)
			end
			
			def <<(theta)
				len = self.length
				@x, @y = len * cos(theta), len * sin(theta)
			end
			
			def >>(phi)
				len = self.length
				px, py = len * cos(get_theta), len * sin(get_theta)
				@z = len * cos(phi)
				@x, @y = px * sin(phi), py * sin(phi)
			end
			
		end
	end
end